package com.bawei.ngpsdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;

public class MainActivity extends AppCompatActivity {
    private static final int PERMISSIONS_REQUEST_LOCATION = 0;
    private FusedLocationProviderClient fusedLocationProviderClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestLocationPermission();
        // 用户授权了定位权限，可以开始获取定位信息
        initLocationClient();
        getLocation();
    }


    // 在适当的地方调用这个方法请求定位权限
    private void requestLocationPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_LOCATION);
        }
    }

    // 处理权限请求回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSIONS_REQUEST_LOCATION: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 用户授权了定位权限，可以开始获取定位信息
                    initLocationClient();
                    getLocation();
                } else {
                    // 用户拒绝了定位权限，处理相应的逻辑
                }
                return;
            }
        }
    }


    // 在合适的地方初始化FusedLocationProviderClient
    private void initLocationClient() {
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    }

    // 获取位置信息
    private void getLocation() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        fusedLocationProviderClient.getLastLocation()
                .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        if (location != null) {

                            // 根据定位结果判断是否虚拟定位
                            boolean isMockLocation = location.isFromMockProvider();//是否实物的
                            if(isMockLocation){
                                // 获取到了位置信息
                                double latitude = location.getLatitude();
                                double longitude = location.getLongitude();

                                // 处理位置信息
                                Toast.makeText(MainActivity.this, "纬度："+latitude+"经度："+longitude, Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(MainActivity.this, "虚拟的", Toast.LENGTH_SHORT).show();
                            }



                        }
                    }
                });
    }
}